package calorie.com.calorie.activity;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.lxh.baselibray.view.TitleView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import calorie.com.calorie.R;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.ReceiveInformationConract;
import calorie.com.calorie.contact.UserInfoContact;
import calorie.com.calorie.response.ResultDTO;
import calorie.com.calorie.response.User;
import calorie.com.calorie.response.UserInfoResponce;
import calorie.com.calorie.util.StaticValue;
import retrofit2.Response;

//修改收货信息
public class ModifyReceivingInformationActivity extends MvpActivity<ReceiveInformationConract.ReceiveInformationPresent> implements ReceiveInformationConract.IReceiveInfo {
    public static final String FROM = "from";
    @BindView(R.id.titleView)
    TitleView titleView;
    @BindView(R.id.et_receive_information)
    EditText etReceiveInformation;
    @BindView(R.id.tv_save)
    TextView tvSave;
    String etHintArr[] = {"输入姓名", "输入手机号", "输入收货地址"};
    String titleArr[] = {"姓名", "手机号", "收货地址"};
    String tvTextArr[] = {"保存", "修改"};
    String data;

    @Override
    public void initData(Bundle savedInstanceState) {

        String receiveName = SPUtils.getString(Constant.RECEIVE_NAME, null, context);//收货姓名
        String receivePhone = SPUtils.getString(Constant.RECEIVE_PHONE, null, context);//收货手机号
        String receiveAddress = SPUtils.getString(Constant.RECEIVE_ADDRESS, null, context);//收货地址

        int from = getIntent().getIntExtra(FROM, -1);
        String titleText = "";
        String etHint = "";
        String saveText = "";
        switch (from) {
            case 1:
                titleText = titleArr[0];
                if (ObjectUtils.isEmpty(receiveName)) {
                    saveText = tvTextArr[0];
                    etHint = etHintArr[0];
                } else {
                    etReceiveInformation.setText(receiveName);
                    saveText = tvTextArr[1];
                }
                break;
            case 2:
                titleText = titleArr[1];
                if (ObjectUtils.isEmpty(receivePhone)) {
                    saveText = tvTextArr[0];
                    etHint = etHintArr[1];
                } else {
                    etReceiveInformation.setText(receivePhone);
                    saveText = tvTextArr[1];
                }
                break;
            case 3:
                titleText = titleArr[2];
                if (ObjectUtils.isEmpty(receiveAddress)) {
                    saveText = tvTextArr[0];
                    etHint = etHintArr[2];
                } else {
                    etReceiveInformation.setText(receiveAddress);
                    saveText = tvTextArr[1];
                }
                break;
        }
        if (!ObjectUtils.isEmpty(etHint)) {
            etReceiveInformation.setHint(etHint);
        }
        tvSave.setText(saveText);
        titleView.setTitleText(titleText);
    }

    @OnClick(R.id.tv_save)
    public void onViewClicked() {
        int from = getIntent().getIntExtra(FROM, -1);

        String saveText = tvSave.getText().toString();
        if (tvTextArr[0].equals(saveText)) {
            data = etReceiveInformation.getText().toString().trim();
            if (data.equals("")) {
                ToastUtils.showLongToast(context, "输入内容不能为空");
                return;
            }
            switch (from) {
                case 1: //保存姓名
                    mPresent.updateReceivingName(data,StaticValue.user.getId());
                    break;
                case 2://保存电话
                    mPresent.updateReceivingPhone(data,StaticValue.user.getId());
                    break;
                case 3://保存收货地址
                    mPresent.updateReceivingAddress(data,StaticValue.user.getId());
                    break;
            }
        } else {
            etReceiveInformation.setText("");
            tvSave.setText(tvTextArr[0]);
            switch (from) {
                case 1: //姓名
                    etReceiveInformation.setHint(etHintArr[0]);
                    break;
                case 2://手机号
                    etReceiveInformation.setHint(etHintArr[1]);
                    break;
                case 3://收货地址
                    etReceiveInformation.setHint(etHintArr[2]);
                    break;
            }

        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_modify_receiving_information;
    }

    @Override
    protected ReceiveInformationConract.ReceiveInformationPresent createPresent() {
        return new ReceiveInformationConract.ReceiveInformationPresent();
    }
    //修改收货姓名返回
    @Override
    public void getUpdateReceivingNameResult(Response<ResultDTO> response) {
        String code = response.body().getCode();
        if (code.equals("200")) {
            SPUtils.putString(Constant.RECEIVE_NAME,data,context);
            ToastUtils.showLongToast(context, "修改收货姓名成功");
            Gson gs = new Gson();
            StaticValue.user = gs.fromJson(gs.toJson(response.body().getData()), User.class);
            this.finish();
        } else {
            ToastUtils.showLongToast(context, "服务器错误");
        }
    }
    //修改收货电话返回
    @Override
    public void getUpdateReceivingPhoneResult(Response<ResultDTO> response) {
        String code = response.body().getCode();
        if (code.equals("200")) {
            SPUtils.putString(Constant.RECEIVE_PHONE,data,context);
            ToastUtils.showLongToast(context, "修改收货电话成功");
            Gson gs = new Gson();
            StaticValue.user = gs.fromJson(gs.toJson(response.body().getData()), User.class);
            this.finish();
        } else {
            ToastUtils.showLongToast(context, "服务器错误");
        }
    }
    //修改收货地址返回
    @Override
    public void getUpdateReceivingAddressResult(Response<ResultDTO> response) {
        String code = response.body().getCode();
        if (code.equals("200")) {
            SPUtils.putString(Constant.RECEIVE_ADDRESS,data,context);
            ToastUtils.showLongToast(context, "修改收货地址成功");
            Gson gs = new Gson();
            StaticValue.user = gs.fromJson(gs.toJson(response.body().getData()), User.class);
            this.finish();
        } else {
            ToastUtils.showLongToast(context, "服务器错误");
        }
    }
}
